package agents;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.myprofile.profile.MuseumItem;
import com.myprofile.profile.ProfileType;

import resource.Tour;
import jade.core.AID;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import behaviours.tourguide.*;

public class TourGuideAgent extends Agent {
	ArrayList<Tour> tours;
	private Tour t1;
	private Tour t2;
	private Tour t3;
        
	public HashMap<AID, ProfileType> customerProfiles;
	private AID curatorAID;
	
	protected void setup() {
		curatorAID = getCuratorAid();
        
        customerProfiles = new HashMap<AID, ProfileType>();
		
		addBehaviour(new RegisterService(this));
		addBehaviour(new ListenForCustomers(this));
	}
	
	private AID getCuratorAid() {
		DFAgentDescription dfd = new DFAgentDescription();
		ServiceDescription sd = new ServiceDescription();
		sd.setType("get museum item information");
		dfd.addServices(sd);
		
		try {
			DFAgentDescription[] dfds = DFService.search(this, dfd);
			return dfds[0].getName();
		} catch (FIPAException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public Tour getT1() {
		return t1;
	}
	
	public Tour getT2() {
		return t2;
	}
	
	public Tour getT3() {
		return t3;
	}
/*	private void createtours() {
		tours = new ArrayList<Tour>();
		
		Inventory inv = new Inventory();
		List<MuseumItem> invItems = inv.getItems();
		
		ObjectFactory factory = new ObjectFactory();
		t1 = factory.createProfileType();
		t2 = factory.createProfileType();
		t3 = factory.createProfileType();
		
		ProfileType.VisitedItems visitedItems = factory.createProfileTypeVisitedItems();
		List<MuseumItem> items = visitedItems.getVisitedItem();
		
		int inventorySize = invItems.size();
		for (int i = 0; i < 15; i++){
			int index = (int)(Math.random()*inventorySize);
			if (items.contains(invItems.get(index))) {
				i--;
				continue;
			} else {
				items.add(invItems.get(index));
			}
		}
		
		t1.setVisitedItems(visitedItems);
		
		List<MuseumItem> interestingItems = new ArrayList<MuseumItem>(30);
		
		String[] interests = {"astronomy", "art", "electrical engineering", "compasses"};
		
		int foundItems = 0;
		int currentIndex = 0;
		while (foundItems < 30){
			MuseumItem item = invItems.get(currentIndex);
			currentIndex++;
			for (int i = 0; i < interests.length; i++){
				if (interests[i].equalsIgnoreCase(item.getMaterial()) ||
					interests[i].equalsIgnoreCase(item.getSubject()) ||
					interests[i].equalsIgnoreCase(item.getObjectType())) {
					
					interestingItems.add(item);
					foundItems++;
					break;
				}		
			}
		}
		
		visitedItems = factory.createProfileTypeVisitedItems();
		items = visitedItems.getVisitedItem();
		
		for (int i = 0; i < 15; i++){
			items.add(interestingItems.get(i));
		}
		
		t2.setVisitedItems(visitedItems);
		
		visitedItems = factory.createProfileTypeVisitedItems();
		items = visitedItems.getVisitedItem();	
		for (int i = 15; i < 30; i++){
			items.add(interestingItems.get(i));
		}
		t3.setVisitedItems(visitedItems);
		
		try {
			//dump profile into an XML file
			JAXBContext jc = JAXBContext.newInstance("com.myprofile.profile");
			Marshaller marshaller = jc.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			marshaller.marshal(t1 , new FileOutputStream("data/tour1.xml"));
			marshaller.marshal(t2, new FileOutputStream("data/tour2.xml"));
			marshaller.marshal(t3, new FileOutputStream("data/tour3.xml"));
		} catch (IOException ioe){
			ioe.printStackTrace();
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}*/

	public void mergeProfiles(AID customer, ProfileType pt) {
		ProfileType customerPT = customerProfiles.get(customer);
		System.out.println("CUSTOMER Pt: " + customerPT);
		if (customerPT.getEducation() == null && pt.getEducation() != null){
			customerPT.setEducation(pt.getEducation());
			customerPT.setAttitude(pt.getAttitude());
			customerPT.setAge(pt.getAge());
			customerPT.setMotivationOfVisit(pt.getMotivationOfVisit());
		} 
		
		if (customerPT.getInterests() == null && pt.getInterests() != null){
			customerPT.setInterests(pt.getInterests());
		}
		
		if (pt.getVisitedItems().getVisitedItem() != null){
                    if (customerPT.getVisitedItems() == null){
                        customerPT.setVisitedItems(new ProfileType.VisitedItems());
                    }
			List<MuseumItem> customerItems = customerPT.getVisitedItems().getVisitedItem();
			
			List<MuseumItem> ptItem = pt.getVisitedItems().getVisitedItem();
			
			for (MuseumItem item : ptItem){
				customerItems.add(item);
			}
		}
                
                System.out.println("New customer information on customer " + customer.getLocalName());
                System.out.println("Education: " + customerPT.getEducation());
                System.out.println("Attitude: " + customerPT.getAttitude());
                System.out.println("Age: " + customerPT.getAge());
                System.out.println("Motivation of visit: " + customerPT.getMotivationOfVisit());
                System.out.println("Interests: ");
                for (int i = 0; i < customerPT.getInterests().getInterest().size(); i++){
                    System.out.print(customerPT.getInterests().getInterest().get(i));
                }
                System.out.println();
	}

	public boolean haveCustomerProfileInformation(AID customer, int i) {
		if (customerProfiles.containsKey(customer)){
			ProfileType pt = customerProfiles.get(customer);
			if (i == 1 && pt.getEducation() != null) {
				return true;
			} else if (i == 2 && pt.getInterests() != null){
				return true;
			}
			
			return false;
		}
		return false;
	}

	public void loadTours(int size) {
		t1 = Tour.parseXMLProfile("data/"+size+"-tour1.xml");
        t1.setTourType(Tour.T1);
        t1.curator = curatorAID;
		t2 = Tour.parseXMLProfile("data/"+size+"-tour2.xml");
        t2.setTourType(Tour.T2);
        t2.curator = curatorAID;
        t3 = Tour.parseXMLProfile("data/"+size+"-tour3.xml");
        t3.setTourType(Tour.T3);
        t3.curator = curatorAID;
	}
}
